﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ServerApp;
using Server;

namespace ServerTest
{
    [TestClass]
    public class ProductTest
    {

        private void ClearCategories()
        {
            InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
            foreach (var item in entity.ProductCategoryLocalizations)
            {
                entity.DeleteObject(item);
            }
            foreach (var item in entity.ProductCategories)
            {
                entity.DeleteObject(item);
            }
            entity.SaveChanges();
        }

        [TestMethod]
        public void AddCategoryTest()
        {
            ClearCategories();
            ProductService ps = new ProductService();
            CategoryLocalizationModel clm = new CategoryLocalizationModel()
            {
                Description = "Phones",
                Language = "en",
                Name = "Phones"
            };
            List<CategoryLocalizationModel> clmList = new List<CategoryLocalizationModel>();
            clmList.Add(clm);
            ps.AddCategory(string.Empty, null, clmList, "ru");
            Assert.AreEqual(1, ServerApp.SingleEntity.GetInstance().ProductCategoryLocalizations.Count());
            Assert.AreEqual(1, ServerApp.SingleEntity.GetInstance().ProductCategories.Count());
        }

        [TestMethod]
        public void DeleteCategoryTest()
        {
            ClearCategories();
            ProductService ps = new ProductService();
            CategoryLocalizationModel clm = new CategoryLocalizationModel()
            {
                Description = "Phones",
                Language = "en",
                Name = "Phones"
            };
            List<CategoryLocalizationModel> clmList = new List<CategoryLocalizationModel>();
            clmList.Add(clm);
            CategoryView categ = ps.AddCategory(string.Empty, null, clmList, "en");
            ps.DeleteCategory(categ.Id);
            Assert.AreEqual(0, ServerApp.SingleEntity.GetInstance().ProductCategoryLocalizations.Count());
            Assert.AreEqual(0, ServerApp.SingleEntity.GetInstance().ProductCategories.Count());
        }

        [TestMethod]
        public void UpdateCategoryTest()
        {
            ClearCategories();
            ProductService ps = new ProductService();
            CategoryLocalizationModel clm = new CategoryLocalizationModel()
            {
                Description = "Phones",
                Language = "en",
                Name = "Phones"
            };
            List<CategoryLocalizationModel> clmList = new List<CategoryLocalizationModel>();
            clmList.Add(clm);
            CategoryView categ = ps.AddCategory(string.Empty, null, clmList, "en");
            clm.Name = "Telephones";
            CategoryView categ2 = ps.UpdateCategory(categ.Id,string.Empty, null, clmList, "en");
            Assert.AreEqual("Telephones",categ2.Name);
           
        }

        [TestMethod]
        public void UpdateSeveralCategoryTest()
        {
            ClearCategories();
            ProductService ps = new ProductService();
            CategoryLocalizationModel clm = new CategoryLocalizationModel()
            {
                Description = "Phones",
                Language = "en",
                Name = "Phones"
            };
            CategoryLocalizationModel clm2 = new CategoryLocalizationModel()
            {
                Description = "Телефон",
                Language = "ru",
                Name = "Телефон"
            };
            List<CategoryLocalizationModel> clmList = new List<CategoryLocalizationModel>();
            clmList.Add(clm);
            clmList.Add(clm2);
            CategoryView categ = ps.AddCategory(string.Empty, null, clmList, "en");
            clm.Name = "Telephones";
            CategoryView categ2 = ps.UpdateCategory(categ.Id, string.Empty, null, clmList, "ru");
            Assert.AreEqual("Телефон", categ2.Name);

        }
    }
}
